package com.iot08.api.service;

import com.alibaba.fastjson.JSONObject;
import com.iot08.common.entity.app.HeartRateRecord;
import com.iot08.common.service.base.BaseService;
import com.iot08.common.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @version 1.0
 * @Description:
 * @author: Rock
 * @time: 2021/3/8 18:33
 */
@Slf4j
@Service
public class HeartRateRecordService extends BaseService<HeartRateRecord> {


    /**
     * @param imei
     * @param value
     * @time: 2021/3/9 16:39
     * @author: Rock
     * @version 1.0
     */
    public HeartRateRecord add(String imei, String value) {
        HeartRateRecord record = new HeartRateRecord();
        record.setImei(imei);
        record.setData(value);
        record.setTimestamp(System.currentTimeMillis() / 1000);
        record.setUpDayStr(DateUtil.dateToStr(new Date(), DateUtil.FORMAT_4));
        record.setExpiryAt(DateUtil.getSystemExpiryAt());
        return this.save(record);
    }

//    /**
//     * @param imei
//     * @param upDayStr
//     * @time: 2021/3/8 19:27
//     * @author: Rock
//     * @version 1.0
//     */
//    public List<Object> listData(String imei, String upDayStr) {
//        List<Object> list = new ArrayList<>();
//        Query query = new Query(Criteria.where("i").is(imei).and("upDayStr").is(upDayStr));
//        List<HeartRateRecord> records = this.find(query.with(Sort.by(Sort.Order.desc("dateTime"))));
//        for (HeartRateRecord heartRateRecord : records) {
//            Map<String, Object> temp = new HashMap<>();
//            temp.put("time", heartRateRecord.getDateTime());
//            temp.put("value", heartRateRecord.getValue());
//            list.add(temp);
//        }
//        return list;
//    }


    /**
     * 根据日期 获取对应天的 心率数据列表
     * @param imei
     * @param upDayStr
     * @time: 4/14/22 4:48 PM
     * @author: Weichao
     * @version 1.0
     */
    public List<JSONObject> listDataWithDay(String imei, String upDayStr) {
        List<JSONObject> list = new ArrayList<>();
        // 对应日期的 0点时间戳 (处理成秒)
        long upDayZoreTime = DateUtil.strToTimestamp(upDayStr, DateUtil.FORMAT_4) / 1000;
        // 对应日期 下一天的 0点时间戳
        long nextDayZoreTime = upDayZoreTime + 86400;
        Criteria criteria = Criteria.where("i").is(imei).andOperator(Criteria.where("t").gte(upDayZoreTime), Criteria.where("t").lte(nextDayZoreTime));
        if (Integer.valueOf(upDayStr.replace("-","")) >= 20220426) {
            //新的用新的查询方法
//            log.info("新的用新的查询方法");
            criteria = Criteria.where("i").is(imei).andOperator(Criteria.where("upDayStr").is(upDayStr));
        }
        List<HeartRateRecord> records = this.find(new Query(criteria));

        for (HeartRateRecord heartRateRecord : records) {
            JSONObject temp = new JSONObject();
            temp.put("timestamp", heartRateRecord.getTimestamp());
            temp.put("data", heartRateRecord.getData());
            list.add(temp);
        }
        return list;
    }


}
